package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.answer.AnswerDao;
import com.his.struts.dao.answer.AnswerImp;
import com.his.struts.dao.question.QuestionDao;
import com.his.struts.dao.question.QuestionImp;
import com.his.struts.form.AnswerForm;
import com.his.struts.form.QuestionForm;
import com.his.struts.form.UserForm;
import com.his.struts.util.PageUtil;

/**
 * 肖伟亮 Creation date: 05-28-2011
 * 
 * 
 */
public class QuestionAction extends DispatchAction {
	 
	QuestionDao dao = new QuestionImp();
	AnswerDao a_dao = new AnswerImp();

	/** 
	 * Method question 提问
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看我的所有提问
	 */
	public ActionForward question(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 得到session中的token
		String token = (String)request.getSession().getAttribute("token");
		// 得到网页中的token
		String newToken = request.getParameter("token");
		// 是否重复提交
		if(newToken != null && newToken.equals(token)){
			return mapping.findForward("question");
		}
		// 把最新token设置到session中
		request.getSession().setAttribute("token", newToken);
		
		QuestionForm questionForm = (QuestionForm) form; 
		// 添加提问												 
		dao.addQuestion(questionForm);
		return mapping.findForward("question");
	}

	/** 
	 * Method myQuestion 我的提问
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看我的所有提问
	 */
	public ActionForward myQuestion(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		UserForm user = (UserForm) session.getAttribute("userinfo");
		/*
		 * 如果用户为空,跳到登陆页
		 */
		if(user == null){
			return mapping.findForward("login");
		}
		// 得到用户id
		int uid = user.getUid();
		 
		String sql = "questions as q inner join userinfo as u on u.uid=q.uid and q.uid ="+uid+" order by qid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		 if(rs != null){
			 // 封装
			 List<QuestionForm> question = dao.getMyQuestions(rs);
			 // 数据放到request中
			 request.setAttribute("myQuestion", question);
		 }
		 
		ActionForward forward = mapping.findForward("myQuestion");
		return forward;
	}

	/** 
	 * Method details 问题详情
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看该问题详情
	 */
	public ActionForward details(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		QuestionForm questionForm = (QuestionForm) form; 
		int qid = questionForm.getQid();
		/*
		 * 如果用户为空,跳到登陆页
		 */
		if(qid == 0){
			return mapping.findForward("myQuestion");
		}
		// 后台参数
		String admin = request.getParameter("admin");
		
		// 取出问题详情 
		QuestionForm question = dao.getQuestions(qid);
		/*
		 * 设置到作用预
		 */
		request.setAttribute("question", question);
		request.setAttribute("qid", qid);
		
		String sql = "answer where a_qid = "+ qid +" order by a_qid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 2, request);
		 
		if(rs != null){
			// 封装
			List<AnswerForm> answer = a_dao.getAnswerBean(rs);
			// 数据放到request中
			request.setAttribute("answer", answer);
		}
		
		/*
		 * 如果后台不为空 跳后台
		 */
		if(admin != null){
			return mapping.findForward("admin_details");
		}
		// 跳前台
		return  mapping.findForward("details");
	}
	
	/** 
	 * Method allQuestion 所有提问
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看所有提问
	 */
	public ActionForward allQuestion(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		 
		String sql ="questions as q inner join " +
					"userinfo as u on q.uid=u.uid order by qid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		
		 if(rs != null){
			 // 封装
			 List<QuestionForm> questionList = dao.getMyQuestions(rs);
			 // 数据放到request中
			 request.setAttribute("questionList", questionList);
		 }
		 
		return mapping.findForward("allQuesion");
	}
	
	/** 
	 * Method deleteQuestion 删除提问
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 所有提问
	 */
	public ActionForward deleteQuestion(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		QuestionForm questionForm = (QuestionForm) form;
		// 取出问题id
		int qid = questionForm.getQid();
		// 删除问题
		dao.deleteQuestion(qid);
		 
		String sql ="questions as q inner join " +
					"userinfo as u on q.uid=u.uid order by qid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		 if(rs != null){
			 // 封装
			 List<QuestionForm> questionList = dao.getMyQuestions(rs);
			 // 数据放到request中
			 request.setAttribute("questionList", questionList);
		 }
		 request.setAttribute("message", "删除成功");
		return mapping.findForward("allQuesion");
	}
	
	
}